﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// news_list 的摘要说明
    /// </summary>
    public class product_list : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        protected int page = 1;
        protected int pageSize = 0;
        public void ProcessRequest(HttpContext context)
        {
            this.pageSize = 30;
            this.page = DTRequest.GetQueryInt("page", 1);

            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.product bll = new BLL.product();
            DataSet ds = bll.GetList(this.pageSize, this.page, "", "add_time desc", out this.totalCount);
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"product_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    string _title = dr["title"].ToString();
                    if (_title.Length > 7)
                    {
                        strTxt.Append(",\"title\":\"" + _title.Substring(0, 7) + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"title\":\"" + _title + "\"");
                    }
                    if (Convert.ToInt32(dr["price"]) > 0)
                    {
                        strTxt.Append(",\"market_price\":\"" + dr["price"] + "元/" + dr["unit"] + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"market_price\":\"面议\"");
                    }
                    string proimg = dr["img_url"].ToString();

                    if (string.IsNullOrEmpty(proimg))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        if (proimg.Contains("shicaiquanzi"))
                        {
                            if (proimg.Contains("http://")) { strTxt.Append(",\"img_url\":\"" + proimg + "\""); }
                            else { strTxt.Append(",\"img_url\":\"http://" + proimg + "\""); }

                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + proimg + "\"");
                        }
                        //if (proimg.Contains("shicaiquanzi"))
                        //{
                        //    strTxt.Append(",\"img_url\":\"http:\\/\\/" + proimg + "\"");
                        //}
                        //else
                        //{
                        //    strTxt.Append(",\"img_url\":\"http:\\/\\/pic.shicaiquanzi.com" + proimg + "\"");
                        //}
                    }

                    if (Convert.ToInt32(dr["company_id"]) > 0)
                    {
                        strTxt.Append(",\"company\":\"" + new BLL.company().GetTitle(Convert.ToInt32(dr["company_id"])) + "\"");
                    }
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            strTxt.Append("}");

            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}